package cc.fs.zhihu.ui

import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem

/**
 * Created by fostion on 2015/10/14.
 */
open class BaseActivity : AppCompatActivity() {

    val myHandler:MyHandler = MyHandler()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId

        if (id == android.R.id.home) {
            finish()
            return true
        }

        return super.onOptionsItemSelected(item)
    }

    fun runDelay(runnable: ()->Unit,delayMillis:Int){
        val message = Message()
        message.what = -1
        message.arg1 = -1
        message.arg2 = -1
        message.obj = runnable
        myHandler.sendMessageAtTime(message,delayMillis.toLong())
    }


    class MyHandler : Handler(){
        override fun dispatchMessage(msg: Message?) {
            if(msg?.what == -1 && msg?.arg1 == -1 && msg?.arg2 == -1){
                (msg?.obj as (()->Unit)).invoke()
                return
            }
            super.dispatchMessage(msg)
        }
    }

}